import 'package:flutter/material.dart';
import 'package:osc/common/constants.dart';
import 'package:osc/common/osc_user_manager.dart';
import 'package:osc/models/pop_result/pop_result.dart';
import 'package:osc/res/colors.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/res/strings.dart';
import 'package:osc/ui/pages/other/about_app_page.dart';
import 'package:osc/utils/navigator_util.dart';

class SettingPage extends StatefulWidget {
  @override
  _SettingState createState() => new _SettingState();
}

class _SettingState extends State<SettingPage> {
//  final String appName = AppConfig.appName;
  final List<Item> items = [Item(0, "关于${AppConfig.appName}", ""), Item(1, "清除缓存", "0.00KB")];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: Text(StringIds.setting),
        centerTitle: true,
      ),
      body: ListView(
          children: <Widget>[
            buildLine(),
            buildItem(context, items.elementAt(0)),
            buildLine(),
            buildItem(context, items.elementAt(1)),
            buildLine(),
            buildLogoutButton(),
          ],
      ),
    );
  }

  buildItem(BuildContext context, Item item){
    return GestureDetector(
      onTap: (){
        NavigatorUtil.pushPage(context, AboutAppPage());
      },
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.symmetric(vertical: Dimens.gap_view_biggest, horizontal: Dimens.gap_activity_horizontal),
        child: Row(
          children: <Widget>[
            Expanded(child: Text(item.title, style: TextStyle(color: Colours.gray_33, fontSize: Dimens.font_sp12),)),
            Text(item.extract, style: TextStyle(color: Colours.gray_66, fontSize: Dimens.font_sp12)),
            buildArrowRight()
          ],
        ),
      ),
    );
  }


  buildLogoutButton(){
    return Row(
      children: <Widget>[
        buildButton(StringIds.exit, Colors.white, Colors.green, EdgeInsets.all(0), (){
          logout();
        })
      ],
    );
  }

  buildButton(String text, Color textColor, Color bg, EdgeInsetsGeometry padding, Function onPressed){
    return Expanded(
        flex: 1,
        child: Padding(
          padding: padding,
          child: FlatButton(
              color: bg,
              onPressed: onPressed,
              child: Text(text, style: TextStyle(fontSize: Dimens.font_sp16, color: textColor),)
          ),
        )
    );
  }

  buildArrowRight(){
    return Padding(
      padding: EdgeInsets.only(left: Dimens.gap_view_normal),
      child: Image.asset("images/icon_arrow_right.png", width: 10,),
    );
  }
  
  buildLine(){
    return Container(color: Colours.divider_big, height: Dimens.gap_divider_height_big,);
  }

  logout() async{
    await OscUserManager.instance.logout();
    PopResult result = PopResult(id: PopResult.ID_LOGOUT, success: true);
    Navigator.pop<PopResult>(context, result);
  }
}

class Item{
  int index;
  String title;
  String extract;

  Item(this.index, this.title, this.extract);
}

